﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_question_type : System.Web.UI.Page
{
    myDataSetTableAdapters.QuestionTypeTableAdapter quesTypeAdapter = new myDataSetTableAdapters.QuestionTypeTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            display();
        }
    }

    protected void display()
    {
        myDataSet.QuestionTypeDataTable quesTypeTable = quesTypeAdapter.GetData();
        GridView1.DataSource = quesTypeTable;
        GridView1.DataBind();
    }

    protected void clear()
    {
        txtId.Text = "";
        txtType.Text = "";
        lblResult.Text = null;
        lblTypeError.Visible = false;
    }

    protected bool checkEmpty()
    {
        bool status = true;
        if (txtType.Text.Trim() == "")
        {
            lblTypeError.Visible = true;
            status = false;
        }
        else
        {
            lblTypeError.Visible = false;
        }
        return status;
    }

    protected void cmdSearchEnable_Click(object sender, EventArgs e)
    {
        display();
        txtId.Enabled = true;
        txtId.BackColor = System.Drawing.Color.White;
        cmdSearch.Text = "Search";
        cmdClear.Visible = true;
        clear();
        if (cmdSearchEnable.Text == "Enable Search Area")
        {
            Panel1.Visible = true;
            cmdSearchEnable.Text = "Disable Search Area";
            cmdAddEnable.Text = "Enable Add Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdSearchEnable.Text = "Enable Search Area";
        }
    }

    protected void cmdAddEnable_Click(object sender, EventArgs e)
    {
        display();
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Add new";
        cmdClear.Visible = true;
        clear();
        if (cmdAddEnable.Text == "Enable Add Area")
        {
            Panel1.Visible = true;
            cmdAddEnable.Text = "Disable Add Area";
            cmdSearchEnable.Text = "Enable Search Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdAddEnable.Text = "Enable Add Area";
        }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        if (cmdSearch.Text == "Search")
        {
            try
            {
                Panel1.Visible = true;
                string strSQL = "SELECT id, type FROM QuestionType where";
                strSQL += " type like N'%" + txtType.Text.Trim() + "%'";
                if (txtId.Text.Trim() != "")
                {
                    strSQL += " and id = " + txtId.Text.Trim();
                }
                string strConnection = ConfigurationManager.ConnectionStrings["EnvironmentSurveyConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(strConnection);
                SqlCommand sqlCmd = new SqlCommand(strSQL, conn);
                SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                if (dt.Rows.Count != 0)
                {
                    if (dt.Rows.Count == 1)
                    {
                        lblResult.Text = "There is 1 result";
                    }
                    else
                    {
                        lblResult.Text = "There are " + dt.Rows.Count + " results";
                    }
                }
                else
                {
                    lblResult.Text = "0 result... Please search again!";
                }
            }
            catch
            {
                RegisterStartupScript("as", "<script>alert('Input wrong format!')</script>");
            }
        }
        else if (cmdSearch.Text == "Add new")
        {
            if (checkEmpty())
            {
                quesTypeAdapter.InsertQuery(txtType.Text.Trim());
                display();
            }
        }
        else
        {
            if (checkEmpty())
            {
                quesTypeAdapter.UpdateQuery(txtType.Text.Trim(), int.Parse(txtId.Text.Trim()));
                display();
            }
        }
    }

    protected void cmdClear_Click(object sender, EventArgs e)
    {
        clear();
        Panel1.Visible = true;
    }

    protected void cmdHide_Click(object sender, EventArgs e)
    {
        display();
        Panel1.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        lblResult.Text = null;
    }

    protected void getDataById(int id)
    {
        myDataSet.QuestionTypeDataTable quesTypeTable = quesTypeAdapter.GetDataById(id);
        txtId.Text = quesTypeTable[0]["id"].ToString();
        txtType.Text = quesTypeTable[0]["type"].ToString();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = true;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Save";
        cmdClear.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        clear();
        getDataById(id);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            quesTypeAdapter.DeleteQuery(id);
            clear();
            display();
        }
        catch
        {
            RegisterStartupScript("as", "<script>alert('Cannot delete!')</script>");
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        display();
    }
}
